﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBKBerget.Models;

namespace DatabaseMerge
{
    public class Program
    {
        static void Main(string[] args)
        {
            new Program();
        }


        public Program()
        {
            InsertMatches();
        }



        private void InsertMatches()
        {

            using (Entities db = new Entities())
            {
                var oldMatches = db.TEMP_matcher.ToList();
                var seasons = db.Seasons.ToList();
                var allteams = db.Teams.ToList();

                foreach (var match in oldMatches)
                {
                    var seasonStartYear = int.Parse(match.season.Replace("/", "").Substring(0, 4));
                    
                    Seasons season = seasons.Where(x => x.StartYear == seasonStartYear).FirstOrDefault();

                    var date = match.datum;
                    string tid = match.tid.Replace('.', ':');
                    if (string.IsNullOrEmpty(tid))
                    {
                        tid = "00:00";
                    }

                    var time = TimeSpan.Parse(tid);
                    var dateTime = date.Add(time);

                    string team1 = string.Empty;
                    string team2 = string.Empty;
                    var teams = match.lag.Split('-');
                    if (teams.Count() != 2)
                    {
                        continue;
                    }
                    team1 = teams[0].Trim();
                    team2 = teams[1].Trim();


                    Teams homeTeam = allteams.Where(x => x.TeamName == team1).FirstOrDefault();
                    Teams awayTeam = allteams.Where(x => x.TeamName == team2).FirstOrDefault();

                    var wo = match.resultat.ToLower().Contains("wo");
                    var canceld = match.resultat.ToLower().Contains("inställd");

                    var resultat = match.resultat.Replace("WO", "").Replace("Inställd", "").Replace("N/A","");

                    int? homeScore = null;
                    int? awayScore = null;
                    if (!string.IsNullOrEmpty(resultat))
                    {
                        var scores = resultat.Split('-');
                        homeScore = scores.Count() == 0 ? 0 : int.Parse(scores[0].Trim());
                        awayScore = scores.Count() == 0 ? 0 : int.Parse(scores[1].Trim());
                    }

                    Matches newMatch = new Matches()
                    {
                        SeasonId = season.SeasonId,
                        MatchDate = dateTime,
                        HomeTeamId = homeTeam.TeamId,
                        AwayTeamId = awayTeam.TeamId,
                        HomeTeamGoals = homeScore,
                        AwayTeamGoals = awayScore,
                        WalkOver = wo,
                        Canceld = canceld,
                        UserId = 2, 
                        CreatedDate = DateTime.Now
                    };

                    db.AddToMatches(newMatch);
                }


                db.SaveChanges();

            }

        }
    }
}
